/*
 * Copyright (c) 2012, Marcin Czekaj
 * All rights reserved.
 *
 * This software is open-source under the BSD license; see LICENSE.TXT for a description.
 */
package pl.edu.uj.fais.graph.io;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

import pl.edu.uj.fais.controller.Controller;
import pl.edu.uj.fais.gui.GraphCanvas;

public class GraphMLFileChooser extends JFileChooser {
    
    private static final long serialVersionUID = 3786422764191346719L;
    
    private static final String EXTENSION = ".graphml";
    
    private static GraphMLFileChooser instance = new GraphMLFileChooser();
    private static Controller controller = Controller.getInstance();
    
    public static File showOpenDialog() {
        
        GraphCanvas canvas = controller.getGraphCanvas();
        
        if (instance.showOpenDialog(canvas) == APPROVE_OPTION) {
            return instance.getSelectedFile();
        }
        
        return null;
    }
    
    public static File showSaveDialog() {
        
        GraphCanvas canvas = controller.getGraphCanvas();
        
        if (instance.showSaveDialog(canvas) == APPROVE_OPTION) {
            
            File file = instance.getSelectedFile();
            
            if (!file.getName().toLowerCase().endsWith(EXTENSION)) {
                String path = file.getAbsolutePath();
                return new File(path + EXTENSION);
            }
            
            return file;
        }
        
        return null;
    }
    
    private GraphMLFileChooser() {
        setFileFilter(new FileFilter() {
            
            @Override
            public String getDescription() {
                return "*" + EXTENSION;
            }
            
            @Override
            public boolean accept(File file) {
            	
                return (file.isDirectory() || file.getName().endsWith(EXTENSION));
            }
        });
    }

	public static int showIncludeDataDialog() {
		return JOptionPane.showConfirmDialog(controller.getGraphCanvas(), "Would you like to include node labels and positions?");
	}
}
